package com.xiaoyu.izf.service;

import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.model.MerchantPayChannel;
import com.xiaoyu.izf.model.MerchantWxPayChannelInfo;
import com.xiaoyu.izf.repository.MerchantPayChannelDao;
import com.xiaoyu.izf.repository.MerchantWxPayChannelDao;
import com.xiaoyu.izf.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

@Service
public class MerchantWxPayChannelInfoService {

    @Autowired
    private MerchantWxPayChannelDao merchantWxPayChannelDao;
    @Autowired
    private MerchantPayChannelDao merchantPayChannelDao;

    @Transactional
    public MerchantWxPayChannelInfo save(MerchantPayChannel panChannel, HttpServletRequest request){
        MerchantWxPayChannelInfo wi = merchantWxPayChannelDao.findOneById(panChannel.getAssociatedId());
        wi.setInsertTime(new Timestamp(System.currentTimeMillis()));
        wi.setPicBankcard(CommonUtil.getWebPicUrl(request,wi.getPicBankcard()));
        wi.setPicBusinessLicence(CommonUtil.getWebPicUrl(request,wi.getPicBusinessLicence()));
        wi.setPicCashier(CommonUtil.getWebPicUrl(request,wi.getPicCashier()));
        wi.setPicDoorhead(CommonUtil.getWebPicUrl(request,wi.getPicDoorhead()));
        wi.setPicIdcardInhand(CommonUtil.getWebPicUrl(request,wi.getPicIdcardInhand()));
        wi.setPicLeaseContract(CommonUtil.getWebPicUrl(request,wi.getPicLeaseContract()));
        wi.setPicLegalpersonIdcardFront(CommonUtil.getWebPicUrl(request,wi.getPicLegalpersonIdcardFront()));
        wi.setPicLegalpersonIdcardReverse(CommonUtil.getWebPicUrl(request,wi.getPicLegalpersonIdcardReverse()));
        wi.setPicManageLocation(CommonUtil.getWebPicUrl(request,wi.getPicManageLocation()));
        wi.setPicOpenAccountLicence(CommonUtil.getWebPicUrl(request,wi.getPicOpenAccountLicence()));
        wi.setPicSettlemanIdcardFront(CommonUtil.getWebPicUrl(request,wi.getPicSettlemanIdcardFront()));
        wi.setPicSettlemanIdcardReverse(CommonUtil.getWebPicUrl(request,wi.getPicSettlemanIdcardReverse()));
        wi.setPicUnlegalpersonPrivateAuthletter(CommonUtil.getWebPicUrl(request,wi.getPicUnlegalpersonPrivateAuthletter()));
        if (StrUtil.isNotBlank(wi.getPicOtherinfo1())){
            wi.setPicOtherinfo1(CommonUtil.getWebPicUrl(request,wi.getPicOtherinfo1()));
        }
        if (StrUtil.isNotBlank(wi.getPicOtherinfo2())){
            wi.setPicOtherinfo2(CommonUtil.getWebPicUrl(request,wi.getPicOtherinfo2()));
        }

        wi = merchantWxPayChannelDao.save(wi);
        panChannel.setOpened(2);
        panChannel.setSettleType(wi.getSettleType());
        merchantPayChannelDao.save(panChannel);
        return wi;
    }

    public MerchantWxPayChannelInfo findOne(int id){
        return merchantWxPayChannelDao.findOneById(id);
    }
}
